package com.ruoyi.factorydata.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.ProductionProject;
import com.ruoyi.factorydata.domain.dto.ProductionProjectDTO;
import com.ruoyi.factorydata.domain.vo.ProductionProjectVo;
import com.ruoyi.factorydata.service.IProductionProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生产项目Controller
 *
 * @author ruoyi
 * @date 2025-07-24
 */
@RestController
@RequestMapping("/projectproduction/projectproduction")
public class ProductionProjectController extends BaseController {
    @Autowired
    private IProductionProjectService productionProjectService;

    /**
     * 查询生产项目列表
     */
    @PreAuthorize("@ss.hasPermi('projectproduction:projectproduction:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductionProject productionProject) {
        startPage();
        List<ProductionProjectVo> list = productionProjectService.selectProductionProjectList(productionProject);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }

    /**
     * 查询用户列表
     */
    @GetMapping("/queryUserList")
    public AjaxResult getUserList(SysUser user) {
        startPage();
        return AjaxResult.success(productionProjectService.selectUserList(user));
    }

    /**
     * 导出生产项目列表
     */
    @PreAuthorize("@ss.hasPermi('projectproduction:projectproduction:export')")
    @Log(title = "生产项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductionProject productionProject) {
        List<ProductionProjectVo> list = productionProjectService.selectProductionProjectList(productionProject);
        ExcelUtil<ProductionProjectVo> util = new ExcelUtil<ProductionProjectVo>(ProductionProjectVo.class);
        util.exportExcel(response, list, "生产项目数据");
    }

    /**
     * 获取生产项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('projectproduction:projectproduction:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productionProjectService.selectProductionProjectById(id));
    }

    /**
     * 新增生产项目
     */
    @PreAuthorize("@ss.hasPermi('projectproduction:projectproduction:add')")
    @Log(title = "生产项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionProjectDTO productionProjectDTO) {
        boolean exist = productionProjectService.lambdaQuery().eq(ProductionProject::getProductionOrderNumber, productionProjectDTO.getProductionOrderNumber()).count() > 0;
        if (exist) {
            return warn("生产订单编号已存在");
        }
        productionProjectDTO.setCreateBy(getUserId().toString());
        return toAjax(productionProjectService.insertProductionProject(productionProjectDTO));
    }

    /**
     * 修改生产项目
     */
    @PreAuthorize("@ss.hasPermi('projectproduction:projectproduction:edit')")
    @Log(title = "生产项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionProjectDTO productionProjectDTO) {
        boolean exists = productionProjectService.lambdaQuery()
                .eq(ProductionProject::getProductionOrderNumber, productionProjectDTO.getProductionOrderNumber())
                .ne(ProductionProject::getId, productionProjectDTO.getId()).count() > 0;
        if (exists) {
            return warn("项目编号已存在");
        }
        productionProjectDTO.setCreateBy(getUserId().toString());
        return toAjax(productionProjectService.updateProductionProject(productionProjectDTO));
    }

    /**
     * 删除生产项目
     */
    @PreAuthorize("@ss.hasPermi('projectproduction:projectproduction:remove')")
    @Log(title = "生产项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productionProjectService.deleteProductionProjectByIds(ids));
    }

    /**
     * 作废项目
     */
    @PreAuthorize("@ss.hasPermi('projectproductioninfo:projectproductioninfo:cancellation')")
    @Log(title = "生产项目", businessType = BusinessType.DELETE)
    @PostMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id) {
        return toAjax(productionProjectService.cancel(id));
    }

    /**
     * 项目完成
     */
    @PreAuthorize("@ss.hasPermi('projectproductioninfo:projectproductioninfo:finish')")
    @Log(title = "生产项目", businessType = BusinessType.DELETE)
    @PostMapping("/finish/{id}")
    public AjaxResult finish(@PathVariable Long id) {
        return toAjax(productionProjectService.finish(id));
    }
}
